package com.sunseagear.wind.modules.test.treeTable.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.modules.test.treeTable.entity.TreeTable;
import com.sunseagear.wind.modules.test.treeTable.entity.query.TreeTableQuery;
import com.sunseagear.wind.modules.test.treeTable.service.ITreeTableService;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * All rights Reserved
 *
 * @version V1.0
 * @package test.treeTable
 * @title: 树表控制器
 * @description: 树表控制器
 * @author:
 * @date: 2025-02-04 12:24:13
 * @copyright: All rights reserved.
 */

@Slf4j
@RestController
@RequestMapping("test/treeTable/TreeTable")
@Log(title = "树表")
@RequiredArgsConstructor
public class TreeTableController extends BaseBeanController {

    private final ITreeTableService treeTableService;

    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     * @param query 要添加的实体
     * @return 操作结果
     */
    @GetMapping(value = "list")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("test:treeTable:TreeTable:list")
    public R<List<TreeTable>> list(TreeTableQuery query) {
        QueryWrapper<TreeTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_date");
        queryWrapper.like(ObjectUtil.isNotEmpty(query.getName()),"name", query.getName());
        queryWrapper.eq(ObjectUtil.isNotEmpty(query.getGeocoding()),"geocoding", query.getGeocoding());
        List<TreeTable> list = treeTableService.selectTreeList(queryWrapper);
        return R.success(list);
    }

    /**
     * 添加
     * @param entity 要添加的实体
     * @return 操作结果
     */
    @PostMapping("add")
    @Log(logType = LogType.INSERT)
    @SaCheckPermission("test:treeTable:TreeTable:add")
    public R<TreeTable> add(@Validated @RequestBody TreeTable entity) {
        treeTableService.save(entity);
        return R.success("添加成功", entity);
    }

    /**
     * 更新
     * @param entity 要更新的实体
     * @return 操作结果
     */
    @PutMapping("update")
    @Log(logType = LogType.UPDATE)
    @SaCheckPermission("test:treeTable:TreeTable:update")
    public R<Void> update(@Validated @RequestBody TreeTable entity) {
        treeTableService.updateById(entity);
        return R.success("更新成功");
    }

    /**
     * 详情
     * @param id 主键ID
     * @return 详情数据
     */
    @GetMapping("detail/{id}")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("test:treeTable:TreeTable:detail")
    public R<TreeTable> detail(@PathVariable("id") Long id) {
        TreeTable treeTable = treeTableService.getById(id);
        return R.success(treeTable);
    }

    /**
     * 删除
     * @param idList 主键集合
     * @return 操作结果
     */
    @DeleteMapping("delete")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("test:treeTable:TreeTable:delete")
    public R<Void> delete(@RequestBody List<Long> idList) {
        treeTableService.removeBatchByIds(idList);
        return R.success("删除成功");
    }
}
